/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.IllegalOrphanException;
import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Historique;
import java.util.ArrayList;
import java.util.Collection;
import com.applicatioN.entities.Exiger2;
import com.applicatioN.entities.Exiger1;
import com.applicatioN.entities.Exiger4;
import com.applicatioN.entities.Exiger3;
import com.applicatioN.entities.OffreDEmploi;
import com.applicatioN.entities.Poste;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class PosteJpaController implements Serializable {

    public PosteJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Poste poste) throws PreexistingEntityException, Exception {
        if (poste.getHistoriqueCollection() == null) {
            poste.setHistoriqueCollection(new ArrayList<Historique>());
        }
        if (poste.getExiger2Collection() == null) {
            poste.setExiger2Collection(new ArrayList<Exiger2>());
        }
        if (poste.getExiger1Collection() == null) {
            poste.setExiger1Collection(new ArrayList<Exiger1>());
        }
        if (poste.getExiger4Collection() == null) {
            poste.setExiger4Collection(new ArrayList<Exiger4>());
        }
        if (poste.getExiger3Collection() == null) {
            poste.setExiger3Collection(new ArrayList<Exiger3>());
        }
        if (poste.getOffreDEmploiCollection() == null) {
            poste.setOffreDEmploiCollection(new ArrayList<OffreDEmploi>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Historique> attachedHistoriqueCollection = new ArrayList<Historique>();
            for (Historique historiqueCollectionHistoriqueToAttach : poste.getHistoriqueCollection()) {
                historiqueCollectionHistoriqueToAttach = em.getReference(historiqueCollectionHistoriqueToAttach.getClass(), historiqueCollectionHistoriqueToAttach.getHistoId());
                attachedHistoriqueCollection.add(historiqueCollectionHistoriqueToAttach);
            }
            poste.setHistoriqueCollection(attachedHistoriqueCollection);
            Collection<Exiger2> attachedExiger2Collection = new ArrayList<Exiger2>();
            for (Exiger2 exiger2CollectionExiger2ToAttach : poste.getExiger2Collection()) {
                exiger2CollectionExiger2ToAttach = em.getReference(exiger2CollectionExiger2ToAttach.getClass(), exiger2CollectionExiger2ToAttach.getExiger2PK());
                attachedExiger2Collection.add(exiger2CollectionExiger2ToAttach);
            }
            poste.setExiger2Collection(attachedExiger2Collection);
            Collection<Exiger1> attachedExiger1Collection = new ArrayList<Exiger1>();
            for (Exiger1 exiger1CollectionExiger1ToAttach : poste.getExiger1Collection()) {
                exiger1CollectionExiger1ToAttach = em.getReference(exiger1CollectionExiger1ToAttach.getClass(), exiger1CollectionExiger1ToAttach.getExiger1PK());
                attachedExiger1Collection.add(exiger1CollectionExiger1ToAttach);
            }
            poste.setExiger1Collection(attachedExiger1Collection);
            Collection<Exiger4> attachedExiger4Collection = new ArrayList<Exiger4>();
            for (Exiger4 exiger4CollectionExiger4ToAttach : poste.getExiger4Collection()) {
                exiger4CollectionExiger4ToAttach = em.getReference(exiger4CollectionExiger4ToAttach.getClass(), exiger4CollectionExiger4ToAttach.getExiger4PK());
                attachedExiger4Collection.add(exiger4CollectionExiger4ToAttach);
            }
            poste.setExiger4Collection(attachedExiger4Collection);
            Collection<Exiger3> attachedExiger3Collection = new ArrayList<Exiger3>();
            for (Exiger3 exiger3CollectionExiger3ToAttach : poste.getExiger3Collection()) {
                exiger3CollectionExiger3ToAttach = em.getReference(exiger3CollectionExiger3ToAttach.getClass(), exiger3CollectionExiger3ToAttach.getExiger3PK());
                attachedExiger3Collection.add(exiger3CollectionExiger3ToAttach);
            }
            poste.setExiger3Collection(attachedExiger3Collection);
            Collection<OffreDEmploi> attachedOffreDEmploiCollection = new ArrayList<OffreDEmploi>();
            for (OffreDEmploi offreDEmploiCollectionOffreDEmploiToAttach : poste.getOffreDEmploiCollection()) {
                offreDEmploiCollectionOffreDEmploiToAttach = em.getReference(offreDEmploiCollectionOffreDEmploiToAttach.getClass(), offreDEmploiCollectionOffreDEmploiToAttach.getReference());
                attachedOffreDEmploiCollection.add(offreDEmploiCollectionOffreDEmploiToAttach);
            }
            poste.setOffreDEmploiCollection(attachedOffreDEmploiCollection);
            em.persist(poste);
            for (Historique historiqueCollectionHistorique : poste.getHistoriqueCollection()) {
                Poste oldNomPosteOfHistoriqueCollectionHistorique = historiqueCollectionHistorique.getNomPoste();
                historiqueCollectionHistorique.setNomPoste(poste);
                historiqueCollectionHistorique = em.merge(historiqueCollectionHistorique);
                if (oldNomPosteOfHistoriqueCollectionHistorique != null) {
                    oldNomPosteOfHistoriqueCollectionHistorique.getHistoriqueCollection().remove(historiqueCollectionHistorique);
                    oldNomPosteOfHistoriqueCollectionHistorique = em.merge(oldNomPosteOfHistoriqueCollectionHistorique);
                }
            }
            for (Exiger2 exiger2CollectionExiger2 : poste.getExiger2Collection()) {
                Poste oldPosteOfExiger2CollectionExiger2 = exiger2CollectionExiger2.getPoste();
                exiger2CollectionExiger2.setPoste(poste);
                exiger2CollectionExiger2 = em.merge(exiger2CollectionExiger2);
                if (oldPosteOfExiger2CollectionExiger2 != null) {
                    oldPosteOfExiger2CollectionExiger2.getExiger2Collection().remove(exiger2CollectionExiger2);
                    oldPosteOfExiger2CollectionExiger2 = em.merge(oldPosteOfExiger2CollectionExiger2);
                }
            }
            for (Exiger1 exiger1CollectionExiger1 : poste.getExiger1Collection()) {
                Poste oldPosteOfExiger1CollectionExiger1 = exiger1CollectionExiger1.getPoste();
                exiger1CollectionExiger1.setPoste(poste);
                exiger1CollectionExiger1 = em.merge(exiger1CollectionExiger1);
                if (oldPosteOfExiger1CollectionExiger1 != null) {
                    oldPosteOfExiger1CollectionExiger1.getExiger1Collection().remove(exiger1CollectionExiger1);
                    oldPosteOfExiger1CollectionExiger1 = em.merge(oldPosteOfExiger1CollectionExiger1);
                }
            }
            for (Exiger4 exiger4CollectionExiger4 : poste.getExiger4Collection()) {
                Poste oldPosteOfExiger4CollectionExiger4 = exiger4CollectionExiger4.getPoste();
                exiger4CollectionExiger4.setPoste(poste);
                exiger4CollectionExiger4 = em.merge(exiger4CollectionExiger4);
                if (oldPosteOfExiger4CollectionExiger4 != null) {
                    oldPosteOfExiger4CollectionExiger4.getExiger4Collection().remove(exiger4CollectionExiger4);
                    oldPosteOfExiger4CollectionExiger4 = em.merge(oldPosteOfExiger4CollectionExiger4);
                }
            }
            for (Exiger3 exiger3CollectionExiger3 : poste.getExiger3Collection()) {
                Poste oldPosteOfExiger3CollectionExiger3 = exiger3CollectionExiger3.getPoste();
                exiger3CollectionExiger3.setPoste(poste);
                exiger3CollectionExiger3 = em.merge(exiger3CollectionExiger3);
                if (oldPosteOfExiger3CollectionExiger3 != null) {
                    oldPosteOfExiger3CollectionExiger3.getExiger3Collection().remove(exiger3CollectionExiger3);
                    oldPosteOfExiger3CollectionExiger3 = em.merge(oldPosteOfExiger3CollectionExiger3);
                }
            }
            for (OffreDEmploi offreDEmploiCollectionOffreDEmploi : poste.getOffreDEmploiCollection()) {
                Poste oldNomPosteOfOffreDEmploiCollectionOffreDEmploi = offreDEmploiCollectionOffreDEmploi.getNomPoste();
                offreDEmploiCollectionOffreDEmploi.setNomPoste(poste);
                offreDEmploiCollectionOffreDEmploi = em.merge(offreDEmploiCollectionOffreDEmploi);
                if (oldNomPosteOfOffreDEmploiCollectionOffreDEmploi != null) {
                    oldNomPosteOfOffreDEmploiCollectionOffreDEmploi.getOffreDEmploiCollection().remove(offreDEmploiCollectionOffreDEmploi);
                    oldNomPosteOfOffreDEmploiCollectionOffreDEmploi = em.merge(oldNomPosteOfOffreDEmploiCollectionOffreDEmploi);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findPoste(poste.getNomPoste()) != null) {
                throw new PreexistingEntityException("Poste " + poste + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Poste poste) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Poste persistentPoste = em.find(Poste.class, poste.getNomPoste());
            Collection<Historique> historiqueCollectionOld = persistentPoste.getHistoriqueCollection();
            Collection<Historique> historiqueCollectionNew = poste.getHistoriqueCollection();
            Collection<Exiger2> exiger2CollectionOld = persistentPoste.getExiger2Collection();
            Collection<Exiger2> exiger2CollectionNew = poste.getExiger2Collection();
            Collection<Exiger1> exiger1CollectionOld = persistentPoste.getExiger1Collection();
            Collection<Exiger1> exiger1CollectionNew = poste.getExiger1Collection();
            Collection<Exiger4> exiger4CollectionOld = persistentPoste.getExiger4Collection();
            Collection<Exiger4> exiger4CollectionNew = poste.getExiger4Collection();
            Collection<Exiger3> exiger3CollectionOld = persistentPoste.getExiger3Collection();
            Collection<Exiger3> exiger3CollectionNew = poste.getExiger3Collection();
            Collection<OffreDEmploi> offreDEmploiCollectionOld = persistentPoste.getOffreDEmploiCollection();
            Collection<OffreDEmploi> offreDEmploiCollectionNew = poste.getOffreDEmploiCollection();
            List<String> illegalOrphanMessages = null;
            for (Historique historiqueCollectionOldHistorique : historiqueCollectionOld) {
                if (!historiqueCollectionNew.contains(historiqueCollectionOldHistorique)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Historique " + historiqueCollectionOldHistorique + " since its nomPoste field is not nullable.");
                }
            }
            for (Exiger2 exiger2CollectionOldExiger2 : exiger2CollectionOld) {
                if (!exiger2CollectionNew.contains(exiger2CollectionOldExiger2)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Exiger2 " + exiger2CollectionOldExiger2 + " since its poste field is not nullable.");
                }
            }
            for (Exiger1 exiger1CollectionOldExiger1 : exiger1CollectionOld) {
                if (!exiger1CollectionNew.contains(exiger1CollectionOldExiger1)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Exiger1 " + exiger1CollectionOldExiger1 + " since its poste field is not nullable.");
                }
            }
            for (Exiger4 exiger4CollectionOldExiger4 : exiger4CollectionOld) {
                if (!exiger4CollectionNew.contains(exiger4CollectionOldExiger4)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Exiger4 " + exiger4CollectionOldExiger4 + " since its poste field is not nullable.");
                }
            }
            for (Exiger3 exiger3CollectionOldExiger3 : exiger3CollectionOld) {
                if (!exiger3CollectionNew.contains(exiger3CollectionOldExiger3)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Exiger3 " + exiger3CollectionOldExiger3 + " since its poste field is not nullable.");
                }
            }
            for (OffreDEmploi offreDEmploiCollectionOldOffreDEmploi : offreDEmploiCollectionOld) {
                if (!offreDEmploiCollectionNew.contains(offreDEmploiCollectionOldOffreDEmploi)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain OffreDEmploi " + offreDEmploiCollectionOldOffreDEmploi + " since its nomPoste field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Historique> attachedHistoriqueCollectionNew = new ArrayList<Historique>();
            for (Historique historiqueCollectionNewHistoriqueToAttach : historiqueCollectionNew) {
                historiqueCollectionNewHistoriqueToAttach = em.getReference(historiqueCollectionNewHistoriqueToAttach.getClass(), historiqueCollectionNewHistoriqueToAttach.getHistoId());
                attachedHistoriqueCollectionNew.add(historiqueCollectionNewHistoriqueToAttach);
            }
            historiqueCollectionNew = attachedHistoriqueCollectionNew;
            poste.setHistoriqueCollection(historiqueCollectionNew);
            Collection<Exiger2> attachedExiger2CollectionNew = new ArrayList<Exiger2>();
            for (Exiger2 exiger2CollectionNewExiger2ToAttach : exiger2CollectionNew) {
                exiger2CollectionNewExiger2ToAttach = em.getReference(exiger2CollectionNewExiger2ToAttach.getClass(), exiger2CollectionNewExiger2ToAttach.getExiger2PK());
                attachedExiger2CollectionNew.add(exiger2CollectionNewExiger2ToAttach);
            }
            exiger2CollectionNew = attachedExiger2CollectionNew;
            poste.setExiger2Collection(exiger2CollectionNew);
            Collection<Exiger1> attachedExiger1CollectionNew = new ArrayList<Exiger1>();
            for (Exiger1 exiger1CollectionNewExiger1ToAttach : exiger1CollectionNew) {
                exiger1CollectionNewExiger1ToAttach = em.getReference(exiger1CollectionNewExiger1ToAttach.getClass(), exiger1CollectionNewExiger1ToAttach.getExiger1PK());
                attachedExiger1CollectionNew.add(exiger1CollectionNewExiger1ToAttach);
            }
            exiger1CollectionNew = attachedExiger1CollectionNew;
            poste.setExiger1Collection(exiger1CollectionNew);
            Collection<Exiger4> attachedExiger4CollectionNew = new ArrayList<Exiger4>();
            for (Exiger4 exiger4CollectionNewExiger4ToAttach : exiger4CollectionNew) {
                exiger4CollectionNewExiger4ToAttach = em.getReference(exiger4CollectionNewExiger4ToAttach.getClass(), exiger4CollectionNewExiger4ToAttach.getExiger4PK());
                attachedExiger4CollectionNew.add(exiger4CollectionNewExiger4ToAttach);
            }
            exiger4CollectionNew = attachedExiger4CollectionNew;
            poste.setExiger4Collection(exiger4CollectionNew);
            Collection<Exiger3> attachedExiger3CollectionNew = new ArrayList<Exiger3>();
            for (Exiger3 exiger3CollectionNewExiger3ToAttach : exiger3CollectionNew) {
                exiger3CollectionNewExiger3ToAttach = em.getReference(exiger3CollectionNewExiger3ToAttach.getClass(), exiger3CollectionNewExiger3ToAttach.getExiger3PK());
                attachedExiger3CollectionNew.add(exiger3CollectionNewExiger3ToAttach);
            }
            exiger3CollectionNew = attachedExiger3CollectionNew;
            poste.setExiger3Collection(exiger3CollectionNew);
            Collection<OffreDEmploi> attachedOffreDEmploiCollectionNew = new ArrayList<OffreDEmploi>();
            for (OffreDEmploi offreDEmploiCollectionNewOffreDEmploiToAttach : offreDEmploiCollectionNew) {
                offreDEmploiCollectionNewOffreDEmploiToAttach = em.getReference(offreDEmploiCollectionNewOffreDEmploiToAttach.getClass(), offreDEmploiCollectionNewOffreDEmploiToAttach.getReference());
                attachedOffreDEmploiCollectionNew.add(offreDEmploiCollectionNewOffreDEmploiToAttach);
            }
            offreDEmploiCollectionNew = attachedOffreDEmploiCollectionNew;
            poste.setOffreDEmploiCollection(offreDEmploiCollectionNew);
            poste = em.merge(poste);
            for (Historique historiqueCollectionNewHistorique : historiqueCollectionNew) {
                if (!historiqueCollectionOld.contains(historiqueCollectionNewHistorique)) {
                    Poste oldNomPosteOfHistoriqueCollectionNewHistorique = historiqueCollectionNewHistorique.getNomPoste();
                    historiqueCollectionNewHistorique.setNomPoste(poste);
                    historiqueCollectionNewHistorique = em.merge(historiqueCollectionNewHistorique);
                    if (oldNomPosteOfHistoriqueCollectionNewHistorique != null && !oldNomPosteOfHistoriqueCollectionNewHistorique.equals(poste)) {
                        oldNomPosteOfHistoriqueCollectionNewHistorique.getHistoriqueCollection().remove(historiqueCollectionNewHistorique);
                        oldNomPosteOfHistoriqueCollectionNewHistorique = em.merge(oldNomPosteOfHistoriqueCollectionNewHistorique);
                    }
                }
            }
            for (Exiger2 exiger2CollectionNewExiger2 : exiger2CollectionNew) {
                if (!exiger2CollectionOld.contains(exiger2CollectionNewExiger2)) {
                    Poste oldPosteOfExiger2CollectionNewExiger2 = exiger2CollectionNewExiger2.getPoste();
                    exiger2CollectionNewExiger2.setPoste(poste);
                    exiger2CollectionNewExiger2 = em.merge(exiger2CollectionNewExiger2);
                    if (oldPosteOfExiger2CollectionNewExiger2 != null && !oldPosteOfExiger2CollectionNewExiger2.equals(poste)) {
                        oldPosteOfExiger2CollectionNewExiger2.getExiger2Collection().remove(exiger2CollectionNewExiger2);
                        oldPosteOfExiger2CollectionNewExiger2 = em.merge(oldPosteOfExiger2CollectionNewExiger2);
                    }
                }
            }
            for (Exiger1 exiger1CollectionNewExiger1 : exiger1CollectionNew) {
                if (!exiger1CollectionOld.contains(exiger1CollectionNewExiger1)) {
                    Poste oldPosteOfExiger1CollectionNewExiger1 = exiger1CollectionNewExiger1.getPoste();
                    exiger1CollectionNewExiger1.setPoste(poste);
                    exiger1CollectionNewExiger1 = em.merge(exiger1CollectionNewExiger1);
                    if (oldPosteOfExiger1CollectionNewExiger1 != null && !oldPosteOfExiger1CollectionNewExiger1.equals(poste)) {
                        oldPosteOfExiger1CollectionNewExiger1.getExiger1Collection().remove(exiger1CollectionNewExiger1);
                        oldPosteOfExiger1CollectionNewExiger1 = em.merge(oldPosteOfExiger1CollectionNewExiger1);
                    }
                }
            }
            for (Exiger4 exiger4CollectionNewExiger4 : exiger4CollectionNew) {
                if (!exiger4CollectionOld.contains(exiger4CollectionNewExiger4)) {
                    Poste oldPosteOfExiger4CollectionNewExiger4 = exiger4CollectionNewExiger4.getPoste();
                    exiger4CollectionNewExiger4.setPoste(poste);
                    exiger4CollectionNewExiger4 = em.merge(exiger4CollectionNewExiger4);
                    if (oldPosteOfExiger4CollectionNewExiger4 != null && !oldPosteOfExiger4CollectionNewExiger4.equals(poste)) {
                        oldPosteOfExiger4CollectionNewExiger4.getExiger4Collection().remove(exiger4CollectionNewExiger4);
                        oldPosteOfExiger4CollectionNewExiger4 = em.merge(oldPosteOfExiger4CollectionNewExiger4);
                    }
                }
            }
            for (Exiger3 exiger3CollectionNewExiger3 : exiger3CollectionNew) {
                if (!exiger3CollectionOld.contains(exiger3CollectionNewExiger3)) {
                    Poste oldPosteOfExiger3CollectionNewExiger3 = exiger3CollectionNewExiger3.getPoste();
                    exiger3CollectionNewExiger3.setPoste(poste);
                    exiger3CollectionNewExiger3 = em.merge(exiger3CollectionNewExiger3);
                    if (oldPosteOfExiger3CollectionNewExiger3 != null && !oldPosteOfExiger3CollectionNewExiger3.equals(poste)) {
                        oldPosteOfExiger3CollectionNewExiger3.getExiger3Collection().remove(exiger3CollectionNewExiger3);
                        oldPosteOfExiger3CollectionNewExiger3 = em.merge(oldPosteOfExiger3CollectionNewExiger3);
                    }
                }
            }
            for (OffreDEmploi offreDEmploiCollectionNewOffreDEmploi : offreDEmploiCollectionNew) {
                if (!offreDEmploiCollectionOld.contains(offreDEmploiCollectionNewOffreDEmploi)) {
                    Poste oldNomPosteOfOffreDEmploiCollectionNewOffreDEmploi = offreDEmploiCollectionNewOffreDEmploi.getNomPoste();
                    offreDEmploiCollectionNewOffreDEmploi.setNomPoste(poste);
                    offreDEmploiCollectionNewOffreDEmploi = em.merge(offreDEmploiCollectionNewOffreDEmploi);
                    if (oldNomPosteOfOffreDEmploiCollectionNewOffreDEmploi != null && !oldNomPosteOfOffreDEmploiCollectionNewOffreDEmploi.equals(poste)) {
                        oldNomPosteOfOffreDEmploiCollectionNewOffreDEmploi.getOffreDEmploiCollection().remove(offreDEmploiCollectionNewOffreDEmploi);
                        oldNomPosteOfOffreDEmploiCollectionNewOffreDEmploi = em.merge(oldNomPosteOfOffreDEmploiCollectionNewOffreDEmploi);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = poste.getNomPoste();
                if (findPoste(id) == null) {
                    throw new NonexistentEntityException("The poste with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Poste poste;
            try {
                poste = em.getReference(Poste.class, id);
                poste.getNomPoste();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The poste with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Historique> historiqueCollectionOrphanCheck = poste.getHistoriqueCollection();
            for (Historique historiqueCollectionOrphanCheckHistorique : historiqueCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Poste (" + poste + ") cannot be destroyed since the Historique " + historiqueCollectionOrphanCheckHistorique + " in its historiqueCollection field has a non-nullable nomPoste field.");
            }
            Collection<Exiger2> exiger2CollectionOrphanCheck = poste.getExiger2Collection();
            for (Exiger2 exiger2CollectionOrphanCheckExiger2 : exiger2CollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Poste (" + poste + ") cannot be destroyed since the Exiger2 " + exiger2CollectionOrphanCheckExiger2 + " in its exiger2Collection field has a non-nullable poste field.");
            }
            Collection<Exiger1> exiger1CollectionOrphanCheck = poste.getExiger1Collection();
            for (Exiger1 exiger1CollectionOrphanCheckExiger1 : exiger1CollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Poste (" + poste + ") cannot be destroyed since the Exiger1 " + exiger1CollectionOrphanCheckExiger1 + " in its exiger1Collection field has a non-nullable poste field.");
            }
            Collection<Exiger4> exiger4CollectionOrphanCheck = poste.getExiger4Collection();
            for (Exiger4 exiger4CollectionOrphanCheckExiger4 : exiger4CollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Poste (" + poste + ") cannot be destroyed since the Exiger4 " + exiger4CollectionOrphanCheckExiger4 + " in its exiger4Collection field has a non-nullable poste field.");
            }
            Collection<Exiger3> exiger3CollectionOrphanCheck = poste.getExiger3Collection();
            for (Exiger3 exiger3CollectionOrphanCheckExiger3 : exiger3CollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Poste (" + poste + ") cannot be destroyed since the Exiger3 " + exiger3CollectionOrphanCheckExiger3 + " in its exiger3Collection field has a non-nullable poste field.");
            }
            Collection<OffreDEmploi> offreDEmploiCollectionOrphanCheck = poste.getOffreDEmploiCollection();
            for (OffreDEmploi offreDEmploiCollectionOrphanCheckOffreDEmploi : offreDEmploiCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Poste (" + poste + ") cannot be destroyed since the OffreDEmploi " + offreDEmploiCollectionOrphanCheckOffreDEmploi + " in its offreDEmploiCollection field has a non-nullable nomPoste field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(poste);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Poste> findPosteEntities() {
        return findPosteEntities(true, -1, -1);
    }

    public List<Poste> findPosteEntities(int maxResults, int firstResult) {
        return findPosteEntities(false, maxResults, firstResult);
    }

    private List<Poste> findPosteEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Poste.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Poste findPoste(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Poste.class, id);
        } finally {
            em.close();
        }
    }

    public int getPosteCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Poste> rt = cq.from(Poste.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
